﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using NIEN_LUAN.Db;
using System.Windows.Forms;

namespace NIEN_LUAN
{
    public partial class FormThongKePhieuThu : Form
    {
        private DataContentDataContext db = new DataContentDataContext();

        public FormThongKePhieuThu()
        {
            InitializeComponent();
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormThongKePhieuThu_Load(object sender, EventArgs e)
        {
            this.dataGridViewPhieuThu.AutoGenerateColumns = false;
            var thongkephieuthu = (from tkpt in db.PHIEUTHUs
                                   orderby tkpt.SoPhieuThu ascending
                                   select tkpt
                                       ).ToList();
            this.pHIEUTHUBindingSource.DataSource = thongkephieuthu;

            var nv = (from m in db.NHANVIENs
                      select m).ToList();
            this.bsMaNV.DataSource = nv;
            this.comboBoxNV.DataSource = bsMaNV;
            this.comboBoxNV.DisplayMember = "MaNhanVien";

            //var ct = (from m in db.CHUNGTUs
            //          select m).ToList();
            //this.bsSoct.DataSource = ct;
            //this.txtSoct.DataSource = bsSoct;
            //this.comboBoxCT.DisplayMember = "SoChungTu"; 
        }

        private void btnThongKe_Click(object sender, EventArgs e)
        {
            if (!this.checkBoxTimkiemSoCT.Checked && this.checkBoxTkiemMNV.Checked)
            {                
                if (string.IsNullOrEmpty(this.comboBoxNV.Text))
                {
                    MessageBox.Show("Chưa chọn mã nhân viên", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                var phieuthu = (from m in db.PHIEUTHUs
                                where m.MaNhanVien == this.comboBoxNV.Text
                                select m).ToList();
                this.pHIEUTHUBindingSource.DataSource = phieuthu;
                this.pHIEUTHUBindingSource.ResetBindings(true);
                return;
            }

            if (this.checkBoxTimkiemSoCT.Checked && !this.checkBoxTkiemMNV.Checked)
            {
                if (string.IsNullOrEmpty(this.txtSoct.Text))
                {
                    MessageBox.Show("Chưa chọn số chứng từ");
                    return;
                }              
                var phieuthu = (from m in db.PHIEUTHUs
                                where m.SoChungTu == this.txtSoct.Text
                                select m).ToList();
                this.pHIEUTHUBindingSource.DataSource = phieuthu;
                this.pHIEUTHUBindingSource.ResetBindings(true);
                return;
            }

            if (this.checkBoxTimkiemSoCT.Checked && this.checkBoxTkiemMNV.Checked)
            {
                if (string.IsNullOrEmpty(this.txtSoct.Text))
                {
                    MessageBox.Show("Chưa chọn số chứng từ");
                    return;
                }
                if (string.IsNullOrEmpty(this.comboBoxNV.Text))
                {
                    MessageBox.Show("Chưa chọn mã nhân viên", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                var phieuthu = (from m in db.PHIEUTHUs
                               where ((m.SoChungTu == this.txtSoct.Text) && (m.MaNhanVien == this.comboBoxNV.Text))
                               select m).ToList();
                this.pHIEUTHUBindingSource.DataSource = phieuthu;
                this.pHIEUTHUBindingSource.ResetBindings(true);
                return;
            }
            if (!this.checkBoxTimkiemSoCT.Checked && !this.checkBoxTkiemMNV.Checked)
            {
                var phieuthu = (from m in db.PHIEUTHUs
                               select m).ToList();
                this.pHIEUTHUBindingSource.DataSource = phieuthu;
                this.pHIEUTHUBindingSource.ResetBindings(true);
                return;
            }
        }

        private void btnChonNV_Click(object sender, EventArgs e)
        {
            Form frmChonNV = new FormTimKiemNhanVien(this);
            frmChonNV.ShowDialog();
        }

        private void buttonChonCT_Click(object sender, EventArgs e)
        {
            Form frmChonCT = new FormTimKiemChungTu(this);
            frmChonCT.ShowDialog();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.checkBoxTimkiemSoCT.Checked && !this.checkBoxTkiemMNV.Checked && !this.checkBoxTkiemNgaylap.Checked)
            {
                if (string.IsNullOrEmpty(this.txtSoct.Text))
                {
                    MessageBox.Show("Chưa nhập số chứng từ", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                var phieuthu = (from m in db.PHIEUTHUs
                                where m.SoChungTu == this.txtSoct.Text
                                select m).ToList();
                this.pHIEUTHUBindingSource.DataSource = phieuthu;
                this.pHIEUTHUBindingSource.ResetBindings(true);
                return;
            }

            if (!this.checkBoxTimkiemSoCT.Checked && this.checkBoxTkiemMNV.Checked && !this.checkBoxTkiemNgaylap.Checked)
            {
                if (string.IsNullOrEmpty(this.comboBoxNV.Text))
                {
                    MessageBox.Show("Chưa nhập mã Nhân viên");
                    return;
                }

                var phieuthu = (from m in db.PHIEUTHUs
                                where m.MaNhanVien == this.comboBoxNV.Text
                                select m).ToList();
                this.pHIEUTHUBindingSource.DataSource = phieuthu;
                this.pHIEUTHUBindingSource.ResetBindings(true);
                return;
            }

            if (!this.checkBoxTimkiemSoCT.Checked && !this.checkBoxTkiemMNV.Checked && this.checkBoxTkiemNgaylap.Checked)
            {
                var phieuthu = (from m in db.PHIEUTHUs
                                where ((m.NgayLap >= this.dateTimePickerNgay1.Value) && (m.NgayLap <= this.dateTimePickerNgay2.Value))
                                select m).ToList();
                this.pHIEUTHUBindingSource.DataSource = phieuthu;
                this.pHIEUTHUBindingSource.ResetBindings(true);
                return;
            }

            if (!this.checkBoxTimkiemSoCT.Checked && this.checkBoxTkiemMNV.Checked && this.checkBoxTkiemNgaylap.Checked)
            {
                if (string.IsNullOrEmpty(this.comboBoxNV.Text))
                {
                    MessageBox.Show("Chưa nhập mã Nhân viên");
                    return;
                }

                var phieuthu = (from m in db.PHIEUTHUs
                                where ((m.MaNhanVien == this.comboBoxNV.Text) && ((m.NgayLap >= this.dateTimePickerNgay1.Value) && (m.NgayLap <= this.dateTimePickerNgay2.Value)))
                                select m).ToList();
                this.pHIEUTHUBindingSource.DataSource = phieuthu;
                this.pHIEUTHUBindingSource.ResetBindings(true);
                return;
            }

            if (this.checkBoxTimkiemSoCT.Checked && !this.checkBoxTkiemMNV.Checked && this.checkBoxTkiemNgaylap.Checked)
            {
                if (string.IsNullOrEmpty(this.txtSoct.Text))
                {
                    MessageBox.Show("Chưa nhập số chứng từ", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                var phieuthu = (from m in db.PHIEUTHUs
                                where ((m.SoChungTu == this.txtSoct.Text) && ((m.NgayLap >= this.dateTimePickerNgay1.Value) && (m.NgayLap <= this.dateTimePickerNgay2.Value)))
                                select m).ToList();
                this.pHIEUTHUBindingSource.DataSource = phieuthu;
                this.pHIEUTHUBindingSource.ResetBindings(true);
                return;
            }

            if (this.checkBoxTimkiemSoCT.Checked && this.checkBoxTkiemMNV.Checked && !this.checkBoxTkiemNgaylap.Checked)
            {
                if (string.IsNullOrEmpty(this.txtSoct.Text))
                {
                    MessageBox.Show("Chưa nhập số chứng từ", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (string.IsNullOrEmpty(this.comboBoxNV.Text))
                {
                    MessageBox.Show("Chưa nhập mã Nhân viên");
                    return;
                }

                var phieuthu = (from m in db.PHIEUTHUs
                                where ((m.SoChungTu == this.txtSoct.Text) && (m.MaNhanVien == this.comboBoxNV.Text))
                                select m).ToList();
                this.pHIEUTHUBindingSource.DataSource = phieuthu;
                this.pHIEUTHUBindingSource.ResetBindings(true);
                return;
            }

            if (this.checkBoxTimkiemSoCT.Checked && this.checkBoxTkiemMNV.Checked && this.checkBoxTkiemNgaylap.Checked)
            {
                if (string.IsNullOrEmpty(this.txtSoct.Text))
                {
                    MessageBox.Show("Chưa nhập số chứng từ", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }

                if (string.IsNullOrEmpty(this.comboBoxNV.Text))
                {
                    MessageBox.Show("Chưa nhập mã Nhân viên");
                    return;
                }

                var phieuthu = (from m in db.PHIEUTHUs
                                where ((m.SoChungTu == this.txtSoct.Text) && (m.MaNhanVien == this.comboBoxNV.Text) && ((m.NgayLap >= this.dateTimePickerNgay1.Value) && (m.NgayLap <= this.dateTimePickerNgay2.Value)))
                                select m).ToList();
                this.pHIEUTHUBindingSource.DataSource = phieuthu;
                this.pHIEUTHUBindingSource.ResetBindings(true);
                return;
            }

            if (!this.checkBoxTimkiemSoCT.Checked && !this.checkBoxTkiemMNV.Checked && !this.checkBoxTkiemNgaylap.Checked)
            {
                var phieuthu = (from m in db.PHIEUTHUs
                                select m).ToList();
                this.pHIEUTHUBindingSource.DataSource = phieuthu;
                this.pHIEUTHUBindingSource.ResetBindings(true);
            }
        }

        private void btnIndsPT_Click(object sender, EventArgs e)
        {
            Form frmChonPT = new FormReportPhieuThu(this);
            frmChonPT.ShowDialog();
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            var phieuthu = this.pHIEUTHUBindingSource.Current as PHIEUTHU;
            if (phieuthu != null)
            {
                Form frmXemPT = new FormLapPhieuThu(this, phieuthu);
                frmXemPT.ShowDialog();
            }
            else
            {
                MessageBox.Show("Chọn phiếu nhập");
            }
        }
    }
}
